package com.nlp.visualization.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WeiboUtils {
    /**
     * 提取微博中的时间
     *
     * @param str
     * @return
     */
    public static String getTimeFromStr(String str) {
        str = str.replace("今天", "2017-10-19");
        Pattern pattern = Pattern.compile("[0-9]{4}[-][0-9]{1,2}[-][0-9]{1,2}[ ][0-9]{1,2}[:][0-9]{1,2}[:][0-9]{1,2}");
        Matcher matcher = pattern.matcher(str);
        String dateStr = "";
        if (matcher.find()) {
            dateStr = matcher.group(0);
        }
        if (dateStr.equals("")) {
            pattern = Pattern.compile("[0-9]{1,2}月[0-9]{1,2}日[ ][0-9]{1,2}[:][0-9]{1,2}");
            matcher = pattern.matcher(str);
            if (matcher.find()) {
                dateStr = matcher.group(0);
                dateStr = dateStr.replace("月", "-");
                dateStr = dateStr.replace("日", "");
                dateStr = "2017-" + dateStr + ":00";
            }

        }
        if (dateStr.equals(""))
            return "2017-10-19 00:00:00";
        return dateStr.toString();
    }


    /**
     * 自定义的Hash方法，返回0-9
     *
     * @param id
     * @return
     */
    public static int getHashValue(String id) {

        return ((id.charAt(0) - '0') + (id.charAt(id.length() - 1) - '0') * 7) % 10;

    }

    public static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        if (StringUtils.isBlank(source)) {
            return source;
        }
        StringBuilder buf = null;
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isEmojiCharacter(codePoint)) {
                if (buf == null) {
                    buf = new StringBuilder(source.length());
                }
                buf.append(codePoint);
            }
        }
        if (buf == null) {
            return source;
        } else {
            if (buf.length() == len) {
                buf = null;
                return source;
            } else {
                return buf.toString();
            }
        }
    }


    public static void saveJoinWeiboByPerson(File outputFile, List<String> list) throws IOException {
        int i = 0;
        String buf = "";
        for (String str : list) {
            if (i % 4096 == 0) {
                FileUtils.write(outputFile, buf, true);
                buf = "";
            } else {
                buf += str + "\n";
            }
            i++;
        }
        FileUtils.write(outputFile, buf, true);
    }
}
